<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'is_agent'=>1);
	if($_RQ['status']){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if($_RQ['fromuser']){
		$params['fromuser']=intval($_RQ['fromuser']);
		$return['params']['fromuser']=$_RQ['fromuser'];
	}
	if($_RQ['gei_mid']){
		$params['gei_mid']=intval($_RQ['gei_mid']);
		$return['params']['gei_mid']=$_RQ['gei_mid'];
	}
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),		
	);
	$list=$this->get_list('member',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['agent']=cfc('core')->member_show($item['fromuser']);
			$item['ab']=cfc('core')->member_show($item['gei_mid']);
			$item['url']=custom_url('jy_merch3fee/myhome/main/mid='.$item['mid'].'&fromuser='.$item['mid'],true);
			$item['appurl']='jy_merch3fee/myhome//mid='.$item['mid'].'&fromuser='.$item['mid'];
			$item['commission_1']=$this->sum('commission',array('merchid'=>$_SESSION['onemerchid'],'status <='=>1,'mid'=>$item['mid']),'money');
			$item['commission_2']=$this->sum('commission',array('merchid'=>$_SESSION['onemerchid'],'status'=>2,'mid'=>$item['mid']),'money');
			$item['isdongfee']=$this->sum('commission',array('merchid'=>$_SESSION['onemerchid'],'isjiedong'=>0,'mid'=>$item['mid']),'dong_fee');
			//$item['order_num']=$this->count('order',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'teamid'=>$item['mid']));
			//$item['order_price']=pdo_getsum('jy_merch3fee_order',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'teamid'=>$item['mid']),'order_price');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'list'=>array('data'=>'list','tip'=>'【温馨提示】：</br>1、所有代理的分红，可以等级里面设置或独立商品里面设置！<red>设置等级，1、会检测上级升级条件！</red>','edit'=>false,'list'=>array(
			array('name'=>'mid','title'=>'MID','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'agent','title'=>'上级信息','type'=>'member'),
			array('name'=>'ab','title'=>'我的老板','type'=>'member'),
			array('name'=>'url','title'=>'推广二维码','type'=>'ewm','action'=>'appurl'),
			array('name'=>'level_id','title'=>'代理等级','type'=>'select','options'=>'levels'),
			array('name'=>'commission_1','title'=>'待结算','type'=>'string'),
			array('name'=>'commission_2','title'=>'已结算','type'=>'string'),
			//array('name'=>'isdongfee','title'=>'冻结中','type'=>'string'),
			//array('name'=>'order_num','title'=>'团订单数','type'=>'string'),
			//array('name'=>'order_price','title'=>'团订单总金额','type'=>'string'),
			//array('name'=>'is_store','title'=>'设为门店主','type'=>'switch','set'=>'set'),
		),'action'=>array(
			array('type'=>'href','op'=>'member','title'=>'他的直推'),
			array('type'=>'modal','op'=>'setlevel','title'=>'设置等级'),
			array('type'=>'modal','op'=>'check','title'=>'审核','show'=>array('status'=>0)),
			array('type'=>'modal','op'=>'detail','title'=>'申请信息'),
		)),
		'search'=>array(
			array('type'=>'radio','width'=>15,'name'=>'status','title'=>'状态','options'=>'agent_status'),
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'fromuser','title'=>'上级'),
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'gei_mid','title'=>'老板'),
		),
	);
	$return['htmls']=$htmls;
	$return['agent_status']=$this->agent_status;
	$return['levels']=$this->getall('level',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'预选代理');
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'is_agent'=>1);
	if($_RQ['status']){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	$order=$this->pdo_getall('member',$params);
	$agent_status=$this->agent_status;
	if($order){
		foreach($order as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['level_title']=$this->pdo_getcolumn('level',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'id'=>$item['level_id']),'title');
			$item['level_title']=$item['level_title']?:'预选代理';
			$item['statustitle']=$agent_status[$item['status']];
			$item['nickname']=$item['member']['nickname'];
			$item['mobile']=$item['member']['mobile'];
			$item['address']=$item['address']?:'无';
			$item['order_price']=$item['order_price']?:'0.00';
		}
	}
	$header=array(
		'nickname'=>'团队昵称',
		'mobile'=>'团队电话',
		'level_title'=>'代理等级',
		'statustitle'=>'代理状态',
	);
	cfc('tools')->export2excel($header,$order,'代理信息');
}
if($_RQ['op']=='check'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$apply_id=intval($post['id']);
		$status=intval($post['status']);
		if(!$status){
			exi('请选择审核通过还是不通过！','error');
		}
		$this->model('member')->check_agent($apply_id,$status,$post['remark']);
		exi('审核成功！','','list');
	}
	$item=$this->get('member',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定代理不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'审核代理申请',
		'edit'=>array(
			array('name'=>'status','title'=>'审核','type'=>'radio','options'=>'agent_status'),
			array('name'=>'remark','title'=>'不通过原因','type'=>'text','show'=>array('status'=>2)),
		),
	);
	$return['agent_status']=$this->agent_status;
	exi($return);
}
if($_RQ['op']=='detail'){
	$id=intval($_RQ['id']);
	$item=$this->get('member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$item['member']=model('core/member')->detail($item['mid'],array('nickname','avatar','realname','mobile','wechat'));
	$item['member']['avatar']=$item['member']['avatar']?:'../core/resource/images/gw-user.png';
	$item['realname']=$item['realname']?:$item['member']['realname'];
	$item['mobile']=$item['mobile']?:$item['member']['mobile'];
	$item['wechat']=$item['realname']?:$item['member']['wechat'];
	$item['diyformfields']=$item['diyformdata'];
	$return['item']=$item;	
	$htmls=array(
		'type'=>'look',
		'desc'=>'代理信息',
	);
	$htmls['look']=array(
			array('type'=>'image','title'=>'头像','name'=>'member.avatar'),
			array('type'=>'string','title'=>'昵称','name'=>'member.nickname'),
			array('type'=>'string','title'=>'真实姓名','name'=>'realname'),
			array('type'=>'string','title'=>'电话','name'=>'mobile'),
			array('type'=>'string','title'=>'微信号','name'=>'wechat'),
			array(
						'title'=>'其他信息',
						'type'=>'array',
						'name'=>'diyformfields',
						'list'=>array(
							array('type'=>'string','name'=>'title','title'=>'表单标题'),	
							array('type'=>'labelarray','name'=>'data','title'=>'表单内容'),	
						),
			)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='setlevel'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$this->update('member',array('level_id'=>intval($post['level_id'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		if($post['fromuser']){
			$this->model('member')->update_agent_level($post['fromuser']);
		}
		exi('设置成功！','','list');
	}
	$item=$this->get('member',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定代理不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'设置团等级',
		'edit'=>array(
			array('name'=>'level_id','title'=>'代理等级','type'=>'select','options'=>'levels'),
		),
	);
	$return['levels']=$this->getall('level',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'预选代理');
	exi($return);
}
if($_RQ['op']=='member'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	if($_RQ['mid']){
		$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'fromuser'=>intval($_RQ['mid']));
	}
	if($_RQ['id']){
		$mid=$this->pdo_getcolumn('member',array('id'=>$_RQ['id']),'mid');
		$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'fromuser'=>$mid);
	}
	$return['params']['mid']=$params['fromuser'];
	//是否分销商
	if(isset($_RQ['is_agent'])){
		$params['is_agent']=intval($_RQ['is_agent']);
		$return['params']['is_agent']=$_RQ['is_agent'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'createtime desc',
	);
	$list=$this->get_list('member',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			//$item['agent']=cfc('core')->member_show($item['agent_id'],'',array('nickname'=>'总店'));
			$team=$this->pdo_get('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
			$item['level_id']=$item['level_id'];
			$item['teamstatus']=$item['teamstatus'];
			$item['order_num']=$item['order_num'];
			$item['order_price']=$item['order_price'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export2',
		'list'=>array('data'=>'list','key'=>'mid','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'address','title'=>'所属地区','type'=>'string'),
			array('name'=>'level_id','title'=>'代理等级','type'=>'select','options'=>'levels'),
			array('name'=>'teamstatus','title'=>'代理状态','type'=>'radio','options'=>'agent_status'),
			array('name'=>'order_num','title'=>'团订单数','type'=>'string'),
			array('name'=>'order_price','title'=>'团订单总金额','type'=>'string'),
		),'action'=>array(
			array('type'=>'href','op'=>'member','title'=>'他的下线'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择上级'),
			array('name'=>'createtime','width'=>15,'title'=>'加入时间','type'=>'time_range'),
		),
	);
	$return['htmls']=$htmls;
	$return['agent_status']=$this->agent_status;
	$return['levels']=$this->getall('level',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'默认等级');
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择代理！','error');
	}
	$post=get_postdata();
	$this->update('member',array('is_store'=>$post['is_store']),array('id'=>$id));	
	exi('设置成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		$this->delete('member',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->delete('member',array('id'=>$id));	
			}
		}
	}
	exi('删除成功！');
}
?>